<?php
header("Content-Type: text/html; charset=iso-8859-1");
class communication{
	var $fields;	
	function communication($id = "",$field = 'id'){	
		$fields = describe('communications');
		$q = "select * from communications where $field = '$id'";
		$r = ExecRequete($q);
		if(!mysql_num_rows($r)){
#			echo "Aucune communication n'a &eacute;t&eacute; trouv&eacute;e";
			foreach($fields as $field => $type) $this->$field = "";
		}
		else{
			$s = LigneSuivante($r);
			foreach($fields as $field => $type){
				$this->$field = $s->$field;
			}
		}
	}
	
	function print_this($check = 0){
		$membre = new membre($this->membre_id);
		$del = ($this->membre_id == check_cookie()) ? "&nbsp;<img src = 'images/icon_delete.png' alt = 'supprimer' width = '12' height = '12' onclick = 'del_communication($this->id)' style = 'cursor:pointer;' />" : "";
		echo "<div id = 'communication_$this->id'>\n";
		$reaction = ($check) ? "<p class = 'center' style = 'font-size: 8pt'><a href = 'equipes.php?id=$this->equipe_id#communications'>r&eacute;agir</a></p>" : "";
		box(CapitalizeLastName(strtolower(($membre->prenom." ".$membre->nom))).$del."<span class = 'info_date'>".(format_date($this->date))."</span>","<div id ='commentaire_$this->id'>".prepare_info($this->corps).$reaction."</div>",'600','smallinfo');
		if($check) echo "<input type = 'hidden' name = '$this->id' value = '$this->id' />\n";
		echo "</div>\n";		
	}
	
	function delete(){
		if(check_cookie() == $this->membre_id){
			$q = "delete from communications where id = '$this->id' limit 1";
			return (ExecRequete($q)) ? 1 : 0;			
		}
		else return 0;
	}
	
	function add(){
		$fields = describe('communications');
		$q = "insert into communications (".(implode(", ", array_keys($fields))).") values (";
		foreach($fields as $field =>  $type){
			$content = ($type == "text") ? "'".(addslashes(prepare_info($this->$field)))."'" : "'".$this->$field."'";
			if(strpos($type,'timestamp') !== FALSE) $content = 'now()';
			$q .= $content.", ";
		}
		$q = substr($q,0,-2).")";
		return (ExecRequete($q)) ? 1 : 0;
	}
	
	function email(){
		$equipe = new equipe($this->equipe_id);
		$membres = $equipe->list_membres();
		$to = "";
		foreach($membres as $id => $membre){
			$to .= ($membre->email) ? $membre->email.", " : "";
		}
		// $to = "robin.liechti@vbcll.ch";
		$user_id = check_cookie();
		$membre = new membre($user_id);
		$from = ($membre->email) ? $membre->email : "vbcll@vbcll.ch";
		$title = "Communication $equipe->nom";
		$content = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/><title>Communication $equipe->nom</title></head><body>".(utf8_encode(stripslashes($this->corps)))."</body></html>";
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
		// $headers .= 'To: Robin <robin.liechti@vtxnet.ch>' . "\r\n";
		$headers .= 'From: VBCLL <'.$from.">\r\n";
     
		mail($to,$title,$content,$headers);		
	}
}

?>